import BaseController from '../../core/controllers/Base.controller';
/*eslint-disable */
import casaderData from '!json-loader!./data.json';
/*eslint-enable */

@Inject('$log', '$state', 'staffService', 'ngDialogService')
class EditWorkerInfoController extends BaseController {
  constructor() {
    super();

    this.view = {
      avatarFile: '',
      attachmentFiles: [],
    };
    const workerId = $state.params.id;
    this.isAddCertificate = false;
    this.$detailQuery(workerId);
    this.init();
    this.casaderData = casaderData;
  }

  casaderPick(supVal, subVal) {
    this.workerDetailInfo.orgPersonBean.nativePlace = supVal.name + subVal.name;
    this.isCasaderExpanded = false;
  }

  $detailQuery(workerId) {
    this.staffService
      .get({ id: workerId })
      .then((res) => {
        this.workerDetailInfo = res.data;
        this.workerDetailInfo.personCollectBean.idCardBean.age =
        this.$ages(this.workerDetailInfo.personCollectBean.idCardBean.birthdayStr);
        this.view.avatarFile = this.workerDetailInfo.personCollectBean.idCardBean.photoId;
        this.view.attachmentFiles = this.workerDetailInfo.orgPersonFileList;
        this.view.attachmentFiles.forEach((item) => {
          item.name = item.fileBean.fileName;
        });
      });
  }

  // 处理年龄(放入共有js)
  /*eslint-disable */
  $ages(str) {
    const year = str.substr(0,4);
    const currentYear = new Date().getFullYear();
    return (currentYear - year);
  }

  // 获得头像src
  $getAvatarSrc() {
    if (this.workerDetailInfo.personCollectBean.idCardBean
      && this.workerDetailInfo.personCollectBean.idCardBean.file
      && this.workerDetailInfo.personCollectBean.idCardBean.file.filePath) {
      const src = `/${this.workerDetailInfo.personCollectBean.idCardBean.file.filePath}`;
      return src;
    } else {
      return require('../../assets/images/' + 'addBt.jpg');
    }
  }

  $addCertify() {
    this.isAddCertificate = true;
    this.certificateListModel = {
      type: '',
      code: '',
      issueUnit: '',
      issueDate: '',
      validPeriod: '',
    };
    this.$log.debug(this.workerDetailInfo, this.workerDetailInfo.certificateList);
    this.workerDetailInfo.certificateList.push(this.certificateListModel);
  }

  // 删除证书
  $cancelCertify(index) {
    this.workerDetailInfo.certificateList.splice(index, 1);
  }

  $saveData(param) {
    this.workerDetailInfo.orgPersonFileList = [];
    this.currentFiles = {};
    this.view.attachmentFiles.forEach((item) => {
      if (item.active) {
        this.currentFiles = {
          type: '3',
          fileId: item.id,
        };
      } else {
        this.currentFiles = {
          type: '3',
          id: item.id,
        };
      }
      this.workerDetailInfo.orgPersonFileList.push(this.currentFiles);
    });
    // if (this.view.avatarFile && this.view.avatarFile.id) {
    //   this.workerDetailInfo.imageId = this.view.avatarFile.id;
    // }
    this.staffService
      .update(this.workerDetailInfo)
      .then((res) => {
        this.ngDialogService.alertSuccess(res.message);
        this.$state.go('workerInfoDetail', { id: res.data.orgPersonBean.id });
      });
  }

  init() {

  }
}

export default EditWorkerInfoController;
